<?php
class Bnowconfig
{

    public $gametype;
    public $tbl = "bnowconfig";
    public $user_id;

    public function Bnowconfig( $gametype, $user_id = 0 )
    {
        global $g_gametype_3d;
        if ( isset( $g_gametype_3d[$gametype] ) )
        {
            $this->gametype = $gametype;
        }
        else
        {
            exit( "error gametype in Bnowconfig" );
        }
        $this->user_id = $user_id;
    }

    public function setVal( $typeno, $amount )
    {
        $mydb = new DB();
        $sql = "UPDATE {$this->tbl} SET amount = {$amount} WHERE gametype={$this->gametype} AND user_id={$this->user_id} AND typeno='{$typeno}' ";
        $mydb->query( $sql );
        if ( !mysql_affected_rows( ) )
        {
            $sql = "INSERT INTO {$this->tbl} (amount,gametype,user_id,typeno) VALUES({$amount},{$this->gametype},{$this->user_id},'{$typeno}')";
            $mydb->query( $sql );
        }
    }

    public function getVal( $typeno )
    {
        $rs = new RecordSet();
        if ( is_array( $typeno ) )
        {
            $typenoAddSQL = " AND typeno IN ('".implode( "','", $typeno )."')";
            $arrFlag = true;
            $reVal = array();
            foreach ( $typeno as $val )
            {
                $reVal[$val] = 0 - 5000;
            }
        }
        else
        {
            $reVal = 0 - 5000;
            $typenoAddSQL = " AND typeno = '{$typeno}'";
            $arrFlag = false;
        }
        $sql = "SELECT amount,typeno FROM {$this->tbl} WHERE gametype={$this->gametype} AND user_id={$this->user_id} {$typenoAddSQL} ";
        $rs->open( $sql, 0 );
        if ( $rs->eof )
        {
            return $reVal;
        }
        if ( $arrFlag )
        {
            while ( !$rs->eof )
            {
                $tempIdx = $rs->f['typeno'];
                $reVal[$tempIdx] = $rs->f['amount'];
                $rs->next();
            }
            return $reVal;
        }
        return $rs->f['amount'];
    }

}

?>